/**
 * @fileOverview tool
 * @author huangtonger@aliyun.com
 */

const Mixin = {};
const Util = require('../util/');

Mixin.AUGMENT = {
  changeAddEdgeModel(model) {
    this.set('addEdgeModel', model);
  },
  cancelAdd() {
    this.set('addType', undefined);
    this.set('addModel', undefined);
    this.changeMode('default');
  },
  beginAdd(type, model) {
    this.set('addType', type);
    this.set('addModel', model);
    this.changeMode('add');
  },
  endAdd() {
    this.set('addType', undefined);
    this.set('addModel', undefined);
    this.changeMode('default');
  },
  delete() {
    const selectedItems = this.getSelected();
    const graph = this.get('_graph');
    Util.each(selectedItems, item => {
      graph.remove(item);
    });
  },
  toBack() {
    const selectedItems = this.getSelected();
    const graph = this.get('_graph');
    selectedItems.sort((a, b) => {
      const aGroup = a.getGraphicGroup();
      const bGroup = b.getGraphicGroup();
      return Util.getIndex(bGroup) - Util.getIndex(aGroup);
    });
    selectedItems.forEach(item => {
      graph.toBack(item);
    });
  },
  toFront() {
    const selectedItems = this.getSelected();
    const graph = this.get('_graph');
    selectedItems.sort((a, b) => {
      const aGroup = a.getGraphicGroup();
      const bGroup = b.getGraphicGroup();
      return Util.getIndex(aGroup) - Util.getIndex(bGroup);
    });
    selectedItems.forEach(item => {
      graph.toFront(item);
    });
  },
  addGroup(model) {
    const graph = this.get('_graph');
    const selectedItems = this.getSelected();
    let inSameGroup = true;
    let originGroup;
    if (selectedItems.length === 0) {
      return;
    }
    if (!model) {
      model = {
        label: '新建分组'
      };
    }
    Util.setId(model);
    graph.add('group', model);
    graph.toFront(model.id);
    const newGroup = graph.find(model.id);

    selectedItems.forEach(item => {
      const parent = item.getParent();
      if (parent) {
        if (originGroup) {
          if (originGroup !== parent) {
            inSameGroup = false;
          }
        } else {
          originGroup = parent;
        }
      }
    });
    if (!inSameGroup) {
      console.warn('add group elements must have the same parent');
      return;
    }
    if (originGroup) {
      model.parent = originGroup.getModel().id;
    }
    selectedItems.forEach(item => {
      graph.update(item, {
        parent: model.id
      });
    });
    const innerEdges = newGroup.getInnerEdges();
    newGroup.deepEach(child => {
      graph.toFront(child);
    });
    innerEdges.forEach(edge => {
      graph.toFront(edge);
    });
  },
  unGroup() {
    const graph = this.get('_graph');
    const selectedItems = this.getSelected();
    const group = selectedItems[0];
    if (selectedItems.length === 1 && Util.isGroup(group)) {
      group.getChildren().forEach(item => {
        graph.update(item, {
          parent: undefined
        });
        if (!item.collapsedParent) {
          item.show();
        }
        if (item.isGroup) {
          item.deepEach(subItem => {
            if (!subItem.collapsedParent) {
              subItem.show();
            }
          });
        }
      });
      graph.remove(group);
    }
  },
  newGroup(model) {
    this.addGroup(model);
  }
};
module.exports = Mixin;
